Skip to content

Fix infinite loop in RenamePropertyWithSameNameAsClass when suffixed name is already taken#1883

Merged
lahma merged 2 commits intomasterfrom
copilot/fix-infinite-loop-in-rename-property
Mar 1, 2026
Merged

Fix infinite loop in RenamePropertyWithSameNameAsClass when suffixed name is already taken#1883
lahma merged 2 commits intomasterfrom
copilot/fix-infinite-loop-in-rename-property

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 1, 2026

RenamePropertyWithSameNameAsClass could loop infinitely when a property shared its class name AND a property named TypeName1 already existed — candidate was computed once before the while loop and never updated on increment.

Changes

  • CSharpGenerator.cs: Update candidate inside the loop body so it reflects the incremented number each iteration
  • GeneralGeneratorTests.cs + snapshot: Add regression test covering the Foo / Foo1 collision scenario, asserting Foo is correctly renamed to Foo2
// Before — candidate is stale, loop never terminates if TypeName1 exists
var candidate = typeName + number;
while (properties.Exists(p => p.PropertyName == candidate))
{
    number++;
}

// After
var candidate = typeName + number;
while (properties.Exists(p => p.PropertyName == candidate))
{
    number++;
    candidate = typeName + number;
}
Original prompt

This section details on the original issue you should resolve

<issue_title>Infinite loop in RenamePropertyWithSameNameAsClass</issue_title>
<issue_description>Hello, I've discovered an infinite loop in the CSharpGenerator.RenamePropertyWithSameNameAsClass method. The problem arises when a property shares the same name as its enclosing type and another property already bears a '1' suffix:

var number = 1;
var candidate = typeName + number;
while (propertyModels.Any(p => p.PropertyName == candidate))
{
    number++;
}

This loop will persist indefinitely under the described condition.</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…n test

Co-authored-by: lahma <171892+lahma@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix infinite loop in RenamePropertyWithSameNameAsClass Fix infinite loop in RenamePropertyWithSameNameAsClass when suffixed name is already taken Mar 1, 2026
Copilot finished work on behalf of lahma March 1, 2026 16:03
@lahma lahma marked this pull request as ready for review March 1, 2026 18:17
@lahma lahma merged commit 53cc802 into master Mar 1, 2026
2 checks passed
This was referenced Apr 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Infinite loop in RenamePropertyWithSameNameAsClass

2 participants